#!/bin/bash
#/*
 #* com.nathulal.AdBlockToggle
 #* 
 #* Copyright (c) 2009 nathulal <inathulal at gmail.com>. All rights reserved.
 #* 
 #* This file is part of iPhoneAdBlockToggle.
 #* 
 #* iPhoneAdBlockToggle is free software: you can redistribute it and/or modify
 #* it under the terms of the GNU General Public License as published by
 #* the Free Software Foundation, either version 3 of the License, or
 #* (at your option) any later version.
 #* 
 #* iPhoneAdBlockToggle is distributed in the hope that it will be useful,
 #* but WITHOUT ANY WARRANTY; without even the implied warranty of
 #* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 #* GNU General Public License for more details.
 #* 
 #* You should have received a copy of the GNU General Public License
 #* along with iPhoneAdBlockToggle.  If not, see <http://www.gnu.org/licenses/>.
 #*/
 
m_username=`whoami|grep .`
if [ "$m_username" != "root" ]
then
	echo "You must be logged in as root."
	exit
fi;

if [ ! -f /var/mobile/Library/SBSettings/Toggles/AdBlock/AdBlock.pid ]
then
	if [ -f /tmp/adblock_hosts ]
	then
		rm /tmp/adblock_hosts
	fi;
	wget http://pogblog.com/hosts -O /tmp/adblock_hosts
	if [ -f /tmp/adblock_hosts ]
	then
		FILESIZE=`stat -c %s /tmp/adblock_hosts`
		echo "Size of new adblock_hosts file is $FILESIZE bytes"
		if [ $FILESIZE -gt 0 ] ;
		then
			mv /tmp/adblock_hosts /var/mobile/Library/SBSettings/Toggles/AdBlock/AdBlockOn_hosts
		else
			rm /tmp/adblock_hosts
		fi;
	fi;
	echo "AdBlock On" > /var/mobile/Library/SBSettings/Toggles/AdBlock/AdBlock.pid
	if [ -f /private/var/mobile/Library/Preferences/UserHostEntries ]
	then
		cat /private/var/mobile/Library/Preferences/UserHostEntries /var/mobile/Library/SBSettings/Toggles/AdBlock/AdBlockOn_hosts > /etc/hosts
	else
		cp /var/mobile/Library/SBSettings/Toggles/AdBlock/AdBlockOn_hosts /etc/hosts
	fi;		
else
	rm /var/mobile/Library/SBSettings/Toggles/AdBlock/AdBlock.pid
	if [ -f /private/var/mobile/Library/Preferences/UserHostEntries ]
	then
		cat /private/var/mobile/Library/Preferences/UserHostEntries /var/mobile/Library/SBSettings/Toggles/AdBlock/AdBlockOff_hosts > /etc/hosts
	else
		cp /var/mobile/Library/SBSettings/Toggles/AdBlock/AdBlockOff_hosts /etc/hosts
	fi;	
fi;

